const TokenKey = 'App-Token'

export function getToken() {
  return uni.getStorageSync(TokenKey)
}

export function setToken(token) {
  return uni.setStorageSync(TokenKey, token)
}

export function removeToken() {
  return uni.removeStorageSync(TokenKey)
}

// 获取用户信息
export function getUserInfo() {
  return new Promise((resolve, reject) => {
    try {
      // 检查token是否存在
      const token = getToken()
      if (!token) {
        console.log('getUserInfo: 用户未登录（无token）')
        reject(new Error('用户未登录'))
        return
      }
      
      // 从storage_data中获取用户信息
      const storageData = uni.getStorageSync('storage_data') || {}
      const userId = storageData['userId'] || ''
      const userName = storageData['user_name'] || ''
      
      console.log('=== getUserInfo调试信息 ===')
      console.log('storage_data:', storageData)
      console.log('userId from storage:', userId)
      console.log('userName from storage:', userName)
      
      // 检查用户ID是否有效
      if (!userId || userId === 'default' || userId === '') {
        console.log('getUserInfo: 用户ID无效，userId:', userId)
        reject(new Error('用户ID无效'))
        return
      }
      
      resolve({ userId, userName })
    } catch (error) {
      console.error('getUserInfo异常:', error)
      reject(error)
    }
  })
}
